<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <!-- <el-form-item label="所属单位" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="请选择部门"
          style="width: 550px;" />
      </el-form-item> -->
      <el-form-item label="所属库区" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请选择所属库区" style="width: 100%;">
          <el-option v-for="item in areaOptions" :key="item.id" :label="item.label" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="编号" prop="cno">
        <el-input v-model="queryParams.cno" placeholder="请输入编号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['wms:cabinetGroup:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['wms:cabinetGroup:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['wms:cabinetGroup:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['wms:cabinetGroup:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cabinetGroupList" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="cno" min-width="60" />
      <!-- <el-table-column label="SN编码" prop="snNumber" min-width="120" fixed="left"/> -->
      <el-table-column label="名称" align="center" prop="name" min-width="120" />

      <!-- <el-table-column label="所属单位" prop="deptName" /> -->
      <!--      <el-table-column label="所属仓库"prop="storeName" min-width="260"/> -->
      <el-table-column label="所在库区" align="center" prop="areaName" min-width="120" />
      <el-table-column label="所属地域" align="center" prop="region" min-width="60">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_put_task_area" :value="scope.row.region" />
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type" min-width="50">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cabinet_group_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cabinet_group_status" :value="scope.row.status" />
        </template>
      </el-table-column> -->
      <el-table-column label="架子数" align="center" prop="cnums" min-width="60">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleCols(scope.row)">{{scope.row.cnums}}[查看]</el-link>
        </template>
      </el-table-column>
      <el-table-column label="格口数" align="center" prop="lnums" min-width="60">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleLocator(scope.row)">{{scope.row.lnums}}[查看]</el-link>
        </template>
      </el-table-column>
      <el-table-column label="财物数量" align="center" prop="totals" min-width="60">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleProperty(scope.row)">{{scope.row.totals}}[查看]</el-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isShow" size="mini" type="text" :icon="scope.row.isSync?'el-icon-loading':'el-icon-refresh'"
            @click="handleSync(scope.row)">{{scope.row.isSync?'同步中':'同步库位'}}</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:cabinetGroup:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCols(scope.row)"
            v-hasPermi="['wms:cabinet:add']">柜列</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['wms:cabinetGroup:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改智能柜对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="880px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="15">
          <el-col>
            <el-form-item label="所属仓库" prop="storeId">
              <el-select v-model="form.storeId" placeholder="请选择所属仓库" style="width: 100%;">
                <el-option v-for="item in storeOptions" :key="item.id" :label="item.label" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="所属库区" prop="areaId">
              <el-select v-model="form.areaId" placeholder="请选择所属库区" style="width: 100%;">
                <el-option v-for="item in areaOptions" :key="item.id" :label="item.label" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用地域" prop="region">
              <el-select v-model="form.region" placeholder="请选择使用地域" style="width: 100%;">
                <el-option v-for="dict in dict.type.wms_put_task_area" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="编号" prop="cno">
              <el-input v-model="form.cno" placeholder="请输入编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择类型" style="width: 100%;">
                <el-option v-for="dict in dict.type.cabinet_group_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.type == '1'">
            <el-form-item label="SN编码" prop="snNumber">
              <el-input v-model="form.snNumber" placeholder="请输入SN编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="seq">
              <el-input v-model="form.seq" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="库房" prop="storeHouse">
              <el-select v-model="form.storeHouse" placeholder="请选择库房" style="width: 100%;">
                <el-option v-for="dict in dict.type.wms_store_house" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCabinetGroup,
    getCabinetGroup,
    delCabinetGroup,
    addCabinetGroup,
    updateCabinetGroup,
    syncCabinet,
    initBean
  } from "@/api/wms/cabinetGroup";
  import {
    treeselect
  } from "@/api/system/dept";
  import {
    storeTreeselect
  } from "@/api/wms/store";
  import {
    storeAreasTree
  } from "@/api/wms/storeArea";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    name: "CabinetGroup",
    dicts: ['cabinet_group_type', 'cabinet_group_status','wms_put_task_area','wms_store_house'],
    components: {
      Treeselect
    },
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 智能柜表格数据
        cabinetGroupList: [],
        //单位数据
        deptOptions: [],
        //仓库数据
        storeOptions: [],
        //库位数据
        areaOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //是否同步
        isSync: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          id: undefined,
          name: undefined,
          cno: undefined,
          snNumber: undefined,
          deptId: undefined,
          storeId: undefined,
          areaId: undefined,
          type: undefined,
          status: undefined
        },
        // 表单参数
        form: {
          shape:'X1',
        },
        // 表单校验
        rules: {
          region: [{
            required: true,
            message: "所在地域不能为空",
            trigger: "blur"
          }],
          cno: [{
            required: true,
            message: "编号不能为空",
            trigger: "blur"
          }],
          snNumber: [{
            required: true,
            message: "SN编码不能为空",
            trigger: "blur"
          }],
          areaId: [{
            required: true,
            message: "归属库区不能为空",
            trigger: "blur"
          }],
          type: [{
            required: true,
            message: "类型不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.queryParams.cabinetType = this.$router.currentRoute.query.cabinetType ? this.$router.currentRoute.query
        .cabinetType : '0'
      this.getList();
      this.getStoreTreeselect();
      this.getTreeselect();
      this.getAreaTreeselect();
    },
    watch: {

    },
    methods: {
      /** 查询智能柜列表 */
      getList() {
        this.loading = true;
        listCabinetGroup(this.queryParams).then(response => {
          this.cabinetGroupList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getInitBean(){
        initBean({cabinetType:'0'}).then(response=>{
           this.form = response.data;
        })
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      /** 查询仓库信息 */
      getStoreTreeselect() {
        storeTreeselect().then(response => {
          this.storeOptions = response.data;
        });
      },
      /** 查询库区信息 */
      getAreaTreeselect() {
        storeAreasTree().then(response => {
          this.areaOptions = response.data;
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          cno: undefined,
          snNumber: undefined,
          deptId: undefined,
          storeId: undefined,
          areaId: undefined,
          type: undefined,
          status: "0",
          pnums: 0,
          cnums: 0,
          lnums: 0,
          shape:'X1',
          seq: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加货柜";
        this.getInitBean();
      },
      handleCols(row) {
        this.$router.push({
          path: '/wms/storeCabinet/cabinet',
          query: {
            pageNum: this.queryParams.pageNum,
            groupId: row.id
          }
        });
      },
      handleLocator(row) {
        this.$router.push({
          path: '/wms/storeLocator/list',
          query: {
            pageNum: this.queryParams.pageNum,
            cabinetGroupId: row.id
          }
        });
      },
      handleProperty(row) {
        this.$router.push({
          path: '/wms/inventories/list',
          query: {
            pageNum: this.queryParams.pageNum,
            cabinetGroupId: row.id,
            status: '0'
          }
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getCabinetGroup(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改货柜";
        });
      },
      handleSync(row) {
        row.isSync = true;
        syncCabinet(row.snNumber, row.id).then(response => {
          if (response.code == 200) {
            this.$modal.msgSuccess("同步成功");
          } else {
            this.$modal.msgError("同步失败");
          }
          row.isSync = false;
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateCabinetGroup(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addCabinetGroup(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除智能柜编号为"' + ids + '"的数据项？').then(() => {
          this.loading = true;
          return delCabinetGroup(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('wms/cabinetGroup/export', {
          ...this.queryParams
        }, `cabinetGroup_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
